#include "purchasedialog.h"
#include <QSqlQueryModel>

PurchaseDialog::PurchaseDialog(QWidget *parent, MainWindow *_MainWindow) :
    QDialog(parent),
    ui(new Ui::PurchaseDialog),
    pMainWindow(_MainWindow)
{
    ui->setupUi(this);
    //setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
    //setFixedSize(this->width(), this->height());
    Qt::WindowFlags flags = Qt::Dialog;
    flags |= Qt::WindowMinMaxButtonsHint;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);
    QDateTime current_date_time=QDateTime::currentDateTime();
    QString current_date=current_date_time.toString("yyyy.MM.dd hh:mm:ss ddd");
    ui->day_label->setText(current_date);
    ui->purchase_pushButton->setEnabled(false);
    ui->purchase_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//设置选择模式，选择单行
    ui->purchase_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选择行为，以行为单位
    ui->purchase_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置为不可编辑
    ui->purchase_tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->pushButton_pre->setEnabled(false);
    this->init();
    if(pageTotal==1)
    {
        ui->pushButton_next->setEnabled(false);
        ui->pushButton_pre->setEnabled(false);
    }
    //加载类别名称
    SqlManage conn;
    ui->comboBox_category->setEditable(false);
    ui->comboBox_category->setCurrentIndex(0);
    QSqlQueryModel *categoryModel = new QSqlQueryModel(this);
    categoryModel->setQuery("select category from category_table");
    ui->comboBox_category->setModel(categoryModel);
    conn.closeSql();
}

PurchaseDialog::~PurchaseDialog()
{
    qDebug()<<"PurchaseDialog 资源释放";
    delete ui;
}

void PurchaseDialog::closeEvent(QCloseEvent *event)
{
    //TODO: 在退出窗口之前，实现希望做的操作
    event = event;
    emit purchaseClose(0);
}

void PurchaseDialog::on_sales_pushButton_clicked()
{
    pMainWindow->sdlg->show();
    this->close();
}

void PurchaseDialog::on_vip_pushButton_clicked()
{
    //pMainWindow->vdlg->update_vipinfo();    //刷新界面数据
    pMainWindow->vdlg->show();
    this->close();

}

void PurchaseDialog::on_exit_pushButton_clicked()
{
    pMainWindow->edlg->show();
    this->close();
}

void PurchaseDialog::updata_time()
{
    ui->day_label->setText(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd"));
}

/**
*@projectName Test
*@brief       刷新界面中的数据
*@author      ccy
*@date        2018-07-12
*/

void PurchaseDialog::update_purchaseinfo(QList<Record_goods> *date)
{
    ui->purchase_tableWidget->clearContents();//先清除掉之前的数据
    ui->purchase_tableWidget->setRowCount(0);//重置行数为0
    ui->purchase_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);//先固定大小以提高渲染速度
    ui->purchase_tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    if(date == NULL)
    {
        date = new QList<Record_goods>;
        *date = dateNow_goods;
    }
    ui->purchase_tableWidget->setRowCount(date->size());
    for(int i = 0; i< date->size(); i++)
    {
        int curRow = ui->purchase_tableWidget->currentRow()+1;
        curRow = i;
        QTableWidgetItem *item0 = new QTableWidgetItem((*date)[i].goods_id);
        QTableWidgetItem *item1 = new QTableWidgetItem((*date)[i].goods_name);
        QTableWidgetItem *item2 = new QTableWidgetItem((*date)[i].category);
        QTableWidgetItem *item3 = new QTableWidgetItem((*date)[i].brand);
        QTableWidgetItem *item4 = new QTableWidgetItem((*date)[i].size);
        QTableWidgetItem *item5 = new QTableWidgetItem((*date)[i].color);
        QTableWidgetItem *item6 = new QTableWidgetItem((*date)[i].number);
        QTableWidgetItem *item7 = new QTableWidgetItem((*date)[i].unit);
        QTableWidgetItem *item8 = new QTableWidgetItem((*date)[i].into_price);
        QTableWidgetItem *item9 = new QTableWidgetItem((*date)[i].sale_price);
        QTableWidgetItem *item10 = new QTableWidgetItem((*date)[i].sum);

        ui->purchase_tableWidget->setItem(curRow,0,item0);
        ui->purchase_tableWidget->setItem(curRow,1,item1);
        ui->purchase_tableWidget->setItem(curRow,2,item2);
        ui->purchase_tableWidget->setItem(curRow,3,item3);
        ui->purchase_tableWidget->setItem(curRow,4,item4);
        ui->purchase_tableWidget->setItem(curRow,5,item5);
        ui->purchase_tableWidget->setItem(curRow,6,item6);
        ui->purchase_tableWidget->setItem(curRow,7,item7);
        ui->purchase_tableWidget->setItem(curRow,8,item8);
        ui->purchase_tableWidget->setItem(curRow,9,item9);
        ui->purchase_tableWidget->setItem(curRow,10,item10);
    }
    ui->purchase_tableWidget->horizontalHeader()->sectionResizeMode(QHeaderView::Interactive);//行宽可自由拖动
    ui->purchase_tableWidget->verticalHeader()->sectionResizeMode(QHeaderView::Interactive);
    ui->purchase_tableWidget->horizontalHeader()->sectionResizeMode(QHeaderView::ResizeToContents);//行宽自适应
    ui->purchase_tableWidget->verticalHeader()->sectionResizeMode(QHeaderView::ResizeToContents);
    ui->purchase_tableWidget->repaint();
}

void PurchaseDialog::init(QString filed)
{
    dateNow_goods.clear();
    dateForword_goods.clear();
    this->pageNow = 1;
    dateNow_goods.clear();
    SqlManage conn;
    QSqlQuery queryNow = conn.queryGetPage("goods",1,filed);
    queryNow.setForwardOnly(true);
    while(queryNow.next())
    {
        Record_goods *record = new Record_goods(&queryNow);
        dateNow_goods.append(*record);
        delete record;
    }
    queryNow = conn.queryGetPage("goods",2,filed);
    while(queryNow.next())
    {
        Record_goods *record = new Record_goods(&queryNow);
        dateForword_goods.append(*record);
        delete record;
    }
    this->pageTotal = conn.getTotalPage("goods");
    ui->lineEdit->setText(QString("%1/%2").arg(pageNow).arg(pageTotal));
    conn.closeSql();
}
//加载某类别对应的品牌名
void PurchaseDialog::on_comboBox_category_currentIndexChanged(const QString &arg1)
{
    SqlManage conn;
    ui->comboBox_category->setEditable(false);
    QSqlQueryModel *model = new QSqlQueryModel(this);
    model->setQuery(QString("select distinct brand from goods where category ='%1'").arg(arg1));
    ui->comboBox_brand->setModel(model);
    conn.closeSql();
}
//加载某品牌对应的商品名称
void PurchaseDialog::on_comboBox_brand_currentIndexChanged(const QString &arg1)
{
    SqlManage conn;
    ui->comboBox_name->setEditable(false);
    QSqlQueryModel *model_name = new QSqlQueryModel(this);
    model_name->setQuery(QString("select distinct goods_name from goods where brand='%1'and category = '%2'")
                         .arg(arg1)
                         .arg(ui->comboBox_category->currentText()));
    ui->comboBox_name->setModel(model_name);
    conn.closeSql();
}
//加载商品其他对应的相应其他信息
void PurchaseDialog::on_comboBox_name_currentIndexChanged(const QString &arg1)
{
    SqlManage conn;
    QSqlQuery query;
    query.exec(QString("select id, size, color, number, unit, into_price, sale_price, sum from goods where goods_name= '%1'")
               .arg(arg1));
    query.next();
    //ui->lineEdit_id->setEnabled(true);
    //ui->lineEdit_id->setReadOnly(true);
    ui->lineEdit_id->setText(query.value("id").toString());
    ui->lineEdit_size->setText(query.value("size").toString());
    ui->lineEdit_color->setText(query.value("color").toString());
    ui->lineEdit_number->setText(query.value("number").toString());
    ui->lineEdit_unit->setText(query.value("unit").toString());
    ui->lineEdit_intoprice->setText(query.value("into_price").toString());
    ui->lineEdit_outprice->setText(query.value("sale_price").toString());
    conn.closeSql();
}
/**
*@projectName Test
*@brief       向数据库里添加一个商品
*@author      ccy
*@date        2018-07-16
*/
void PurchaseDialog::on_pushButton_add_clicked()
{
    //为避免关闭对话框时主窗口被关闭，必须传入this指针，或者采用main（）函数中的策略
    PurchaseEditDialog *editDlg = new PurchaseEditDialog(this);
    editDlg->setWindowTitle(tr("商品信息的添加"));
    //editDlg->setEditMode(true);
    editDlg->exec();
    SqlManage conn;
    this->pageTotal = conn.getTotalPage("goods");
    conn.closeSql();
    this->init();
    this->update_purchaseinfo();
    delete editDlg;
    ui->lineEdit->setText(QString("%1/%2").arg(pageNow).arg(pageTotal));
}
